<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-22 22:47:46
 * @LastEditTime: 2025-01-26 17:26:54
 * @FilePath: /env-manager-pc/src/views/assets/statistics/index.vue
-->
<script setup lang="ts">
import detail from './detail.vue'
import { useAssetModelClassifys, useAssetModels, useOwnerOrgs, useStateTypes } from '@/utils/dataHook'
import { QueryObj } from '@/utils/dataHook/data'
import { usePage } from '@/utils/commonHook'
import assetsApi from '@/api/modules/assets'
import type { orderForm } from '@/api/modules/types/assets'
import { ModelType } from '@/types/common'

const searchData = reactive(new QueryObj({
  objType: 'DIM.FilterCollection',
  Filters: [{
    Type: 'DIM.Assets.Filters.AssetCodeFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetCodeFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.OwnerOrgGUIDFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.OwnerOrgGUIDFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Business.AssetFilters.EntNameFilter',
    FilterKey: 'Enterprise',
    valueKey: 'Enterprise',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetModelFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetModelFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetStateFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetStateFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.Assets.Filters.AssetModelClassifyFilter',
    FilterKey: '',
    valueKey: 'DIM.Assets.Filters.AssetModelClassifyFilter',
    mutlip: true,
  },
  {
    Type: 'DIM.GYHR.AssetFilters.OrderFormIDFilter',
    FilterKey: 'OFID',
    valueKey: 'OFID',
  },
  ],
}))
const { page, pageNumChange, pageSizeChange } = usePage(getTableData)

const ModelClassifys = useAssetModelClassifys()
const AssetModels = useAssetModels()
const OwnerList = useOwnerOrgs()
const stateTypes = useStateTypes()
const defaultProps = {
  children: 'Children',
  label: 'Name',
}
const detailRef = ref()

function search() {
  page.PageNum = 1
  getTableData()
}
const tableData = ref<orderForm[]>([])
const loading = ref(false)

async function getTableData() {
  loading.value = true
  const res = await assetsApi.GetAssets({
    objFilter: searchData.getQuery(),
    objType: searchData.objType,
    pageSize: page.PageSize,
    pageNum: page.PageNum,
  })
  loading.value = false
  const { Rows, RecordsetCount } = res
  tableData.value = Rows || []
  page.RecordsetCount = RecordsetCount
}

function toDetail(row: orderForm) {
  detailRef.value.open({ type: ModelType.edit, data: row })
}

getTableData()
</script>

<template>
  <div>
    <PageHeader>
      <template #content>
        <ElForm label-width="100">
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="设备编码">
                <ElInput v-model="searchData['DIM.Assets.Filters.AssetCodeFilter']" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="订单号">
                <ElInput v-model="searchData.OFID" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="运维公司">
                <el-tree-select
                  v-model="searchData['DIM.Assets.Filters.OwnerOrgGUIDFilter']"
                  value-key="Code"
                  class="w-full"
                  :data="OwnerList"
                  :render-after-expand="false"
                  :props="defaultProps"
                  clearable check-strictly
                />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="企业名称">
                <ElInput v-model="searchData.Enterprise" clearable />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="型号">
                <ElSelect v-model="searchData['DIM.Assets.Filters.AssetModelFilter']" class="w-full" clearable>
                  <ElOption v-for="item in AssetModels" :key="item.ObjID" :label="item.Name" :value="item.ObjID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="资产状态">
                <ElSelect v-model="searchData['DIM.Assets.Filters.AssetStateFilter']" class="w-full" clearable>
                  <ElOption v-for="item in stateTypes" :key="item.StateID" :label="item.Name" :value="item.StateID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="产品名称">
                <ElSelect v-model="searchData['DIM.Assets.Filters.AssetModelClassifyFilter']" class="w-full" clearable>
                  <ElOption v-for="item in ModelClassifys" :key="item.ObjID" :label="item.Name" :value="item.ObjID" />
                </ElSelect>
              </ElFormItem>
            </ElCol>
            <ElCol :span="6" class="flex flex-justify-end">
              <ElButton type="primary" @click="search">
                查询
              </ElButton>
              <ElButton type="warning">
                导出
              </ElButton>
            </ElCol>
          </ElRow>
        </ElForm>
      </template>
    </PageHeader>
    <PageMain>
      <div v-auto-height="85">
        <vxe-table
          auto-resize border
          height="100%"
          :data="tableData"
          :loading="loading"
          :row-config="{ isCurrent: true }"
        >
          <VxeColumn type="seq" title="序号" width="150" />
          <VxeColumn field="OrderFormCode" title="订单号" width="120" />
          <VxeColumn field="AssetModelClassifyName" title="产品名称" min-width="150" />
          <VxeColumn field="AssetModelName" title="型号" width="150" />
          <VxeColumn field="OrderFormCode" title="设备编码" width="150">
            <template #default="{ row }">
              <el-button type="primary" link @click="toDetail(row)">
                {{ row.AssetCode }}
              </el-button>
            </template>
          </VxeColumn>
          <VxeColumn field="StateName" title="资产状态" width="140" />
          <VxeColumn field="OwnerOrgName" title="运维公司" width="140" />
          <VxeColumn field="EntName" title="企业名称" min-width="180" show-overflow />
          <VxeColumn field="DeviceName" title="设备名称" min-width="220" show-overflow />
          <VxeColumn field="InWarehouseTime" title="入库时间" width="180" />
          <VxeColumn field="UseTime" title="领用时间" width="180" />
          <VxeColumn field="RecAddress" title="安装（销售）时间" width="180">
            <template #default="{ row }">
              {{ row.InstallTime || row.SellTime }}
            </template>
          </VxeColumn>
          <VxeColumn field="InvalidTime" title="拆除时间" width="150" />
        </vxe-table>
      </div>
      <ElPagination
        class="mt-4"
        :page-size="page.PageSize"
        :total="page.RecordsetCount"
        :current-page="page.PageNum"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 50, 100]"
        @current-change="pageNumChange"
        @size-change="pageSizeChange"
      />
    </PageMain>
    <detail ref="detailRef" />
  </div>
</template>
